package com.syqc.choose.teamcar

import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.syqc.comlib.base.LazyFragment
import com.syqc.comlib.utils.setResultTo
import com.syqc.comlib.utils.toast
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragmentSearchRecyclerBinding
import com.syqc.utils.Keys
import com.syqc.utils.ToolsUtil
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers

/**
 *@author joker
 *@time   2024/6/1 10:41
 *@desc
 */
class SearchTeamFragment :
    LazyFragment<FragmentSearchRecyclerBinding>(FragmentSearchRecyclerBinding::inflate) {
    private var isMap: Boolean = false
    private var isChoose: Boolean = false
    private val mAdapter by lazy { TeamsAdapter() }
    private val mViewModel: TeamsVM by activityViewModels<TeamsVM>()

    companion object {
        fun instance(isMap: Boolean, isChose: Boolean): SearchTeamFragment {
            val fragment = SearchTeamFragment()
            fragment.isMap = isMap
            fragment.isChoose = isChose
            return fragment
        }
    }

    override fun initView(view: View) {
        binding.rvInfo.layoutManager = LinearLayoutManager(requireContext())
        binding.rvInfo.adapter = mAdapter
        mAdapter.isChoose = isChoose
    }

    override fun initData() {
        mViewModel.searchTeam("")
        mViewModel.toastObserver().observeOn(AndroidSchedulers.mainThread())
            .autoDispose(scopeProvider).subscribe(::toastObserver)

        mViewModel.searchLive.observe(this) {
            if (it.isEmpty()) {
                R.string.toast_no_find_team.toast(requireContext())
            } else {
                mAdapter.setList(it)
            }
        }
    }

    override fun initEvent() {
        binding.tvFilter.setOnClickListener {
            mViewModel.searchTeam(binding.editSearch.text.toString())
        }

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (ToolsUtil.isFastClick()) return@setOnItemChildClickListener
            val teamInfo = mAdapter.data[position]
            if (isChoose && teamInfo.noAuth) {
                R.string.toast_team_noAuth.toast(requireContext())
                return@setOnItemChildClickListener
            }
            if (!isChoose && teamInfo.carNum <= 0) {
                R.string.toast_choose_no_car.toast(requireContext())
                return@setOnItemChildClickListener
            }
            if (isMap && teamInfo.carNum > 500) {
                R.string.toast_team_cars_much.toast(requireContext())
                return@setOnItemChildClickListener
            }
            setResultTo(requireActivity()) {
                putExtra(Keys.INTENT_TEAM_ID, teamInfo.teamId)
                putExtra(Keys.INTENT_TEAM_NUM, teamInfo.carNum.toString())
                putExtra(Keys.INTENT_TEAM_NAME, teamInfo.teamName)
                putExtra(Keys.INTENT_NO_AUTH, teamInfo.noAuth)
            }
            requireActivity().finish()
        }
    }

}